//
//  YSPWelcomeController.swift
//  Weibo Demo
//
//  Created by 朴裕识 on 15/12/17.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SDWebImage

class YSPWelcomeController: UIViewController {
    
    private var iconViewConstraint: NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        //加载上一次保存的头像
        setIconView()
        YSPUserAccount.userAccount?.loadUserInfo({ (error) -> () in
            if error != nil {
                return
            }
            self.setIconView()
            self.welcomeLabel.text = (YSPUserAccount.userAccount?.screen_name)! as String + "\n欢迎回来"
        })
        
    }
    
    func setIconView() {
        if let iconViewImageURLString = YSPUserAccount.userAccount?.avatar_large {
//            iconView.setimagewithurl
            iconView.setImageWithURLString(iconViewImageURLString)
        }
    }
    
    ///在view显示的时候做动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        self.iconViewConstraint?.constant = 120
        UIView.animateWithDuration(3, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 2, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.view.layoutIfNeeded()
//            self.view.setNeedsLayout()
            }) { (_) -> Void in
                self.welcomeLabel.alpha = 0
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLabel.hidden = false
                    self.welcomeLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        (UIApplication.sharedApplication().delegate as! AppDelegate).switchViewController(true)
                })
        }
    }
    
    func prepareUI() {
        
        view.addSubview(backgroundView)
        view.addSubview(welcomeLabel)
        view.addSubview(iconView)
        
        //开始设置约束
        backgroundView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        //backgroundView全屏
        //top
        view.addConstraint(NSLayoutConstraint(item: backgroundView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //bottom
        view.addConstraint(NSLayoutConstraint(item: backgroundView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        //leading
        view.addConstraint(NSLayoutConstraint(item: backgroundView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //trailing
        view.addConstraint(NSLayoutConstraint(item: backgroundView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        //iconView
        //centerX = view.centerX
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //width = 85
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        //height = 85
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        //centerY = view.top + 400
        iconViewConstraint = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 400)
        view.addConstraint(iconViewConstraint!)
        
        //welcomeLabel
        //centerX = view.centerX
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //top = iconView.bottom + 10
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
    }
    
    //MARK: - 懒加载
    //背景
    lazy var backgroundView = UIImageView(image: UIImage(named: "ad_background"))

    //textLabel
    lazy var welcomeLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎回来"
        label.textColor = UIColor.darkGrayColor()
        label.hidden = true
        return label
    }()
    
    //头像
    lazy var iconView: UIImageView = {
        let image = UIImage(named: "avatar_default_big")
        let iconView = UIImageView(image: image)
        iconView.layer.cornerRadius = (image?.size.width)! / 2
        iconView.layer.masksToBounds = true
        return iconView
    }()
    
    
    
    
    
    
    
    
    
    
    
    
}
